﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JCTimes.DomainModel.Repository;
using JCTimes.DomainModel.Views;
using JCTimes.DomainModel.Entity;
using JCTimes.Portal.Infrastructure;
using JCTimes.Portal.Models;
using JCTimes.Portal.Specification;
using System.Data;

namespace JCTimes.Portal.Controllers
{
    [Permission]
    public class PDAVersionController : Controller
    {

        private IPDAVersionsRepository pvRep = null;
        private const int pageSize = 10;

        public PDAVersionController(IPDAVersionsRepository pvRep)
        {
            this.pvRep = pvRep;
        }

        //
        // GET: /PDAVersion/

        public ActionResult Index()
        {
            return View();
        }


        /// <summary>
        /// Query users.
        /// </summary>
        /// <param name="pageIndex">Page index.</param>
        /// <param name="searcher">Filter conditions.</param>
        /// <returns>Users grid view</returns>
        public PartialViewResult Grid(int? pageIndex, Searcher searcher)
        {
            var model = new GridView<PDAVersion>
            {
                Searcher = searcher,
                PagedList = pvRep.GetList<PDAVersion>().OrderByDescending(u => u.CreateDate).ToPagedList(pageIndex ?? 1, pageSize)
            };
            return PartialView("_gridview", model);
        }

        public ActionResult Create()
        {
            return View(new PDAVersion { ID = Guid.NewGuid().ToString() });
        }

        /// <summary>
        /// Add user.
        /// </summary>
        /// <param name="user">User item</param>
        /// <returns>Add result in json</returns>
        [HttpPost]
        public JsonResult Create(PDAVersion pda)
        {
            try
            {
                pda.CreateDate = DateTime.Now;
                pvRep.Add(pda);
                pvRep.SaveChanges();
                return Json(new { Msg = "添加成功", IsSuccess = true });
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.Message, ex);
                return Json(new { Msg = "出错了" });
            }
        }

        /// <summary>
        /// Delete user.
        /// </summary>
        /// <param name="id">User id.</param>
        /// <param name="pageIndex">Page index.</param>
        /// <param name="searcher">Filter conditions</param>
        /// <returns></returns>
        public RedirectToRouteResult Delete(string id, int pageIndex, Searcher searcher)
        {
            try
            {
                var pdaversion = pvRep.Find(id);
                pvRep.Delete(pdaversion);
                pvRep.SaveChanges();
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.Message, ex);
            }
            return RedirectToAction("Grid", new { pageIndex = pageIndex, searcher = searcher });
        }
    }
}
